//
//  ZAlertControlTool.m
//  ZPrograme
//
//  Created by 周成龙 on 16/6/16.
//  Copyright © 2016年 none. All rights reserved.
//

#import "ZAlertControlTool.h"
#import <objc/runtime.h>
static NSString *UIAlertViewKey = @"UIAlertViewKey";

@implementation ZAlertControlTool
+ (void)showHUB:(NSString *)msg
{
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                  message:msg
                                                 delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
    [alert show];
}

+ (void)showHUB:(NSString *)msg withTitle:(NSString *)title
{
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:title
                                                  message:msg
                                                 delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
    [alert show];
}

+ (void)showConfirmHUB:(NSString *)msg
{
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                  message:msg
                                                 delegate:nil
                                        cancelButtonTitle:@"否"
                                        otherButtonTitles:@"是",nil];
    [alert show];
}

+ (void)showHUB:(NSString *)msg withTitle:(NSString *)title back:(UINavigationController *)navigationVC
{
    [[self class] showHUB:msg withTitle:title];
}

#pragma mark - iOS8 early
- (void)alertWithCallBackBlock:(UIAlertViewCallBackBlock)alertViewCallBackBlock
                         title:(NSString *)title
                       message:(NSString *)message
              cancelButtonName:(NSString *)cancelButtonName
             otherButtonTitles:(NSString *)otherButtonTitles, ...NS_REQUIRES_NIL_TERMINATION
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonName otherButtonTitles: otherButtonTitles, nil];
    NSString *other = nil;
    va_list args;
    if (otherButtonTitles) {
        va_start(args, otherButtonTitles);
        while ((other = va_arg(args, NSString*))) {
            [alert addButtonWithTitle:other];
        }
        va_end(args);
    }
    alert.delegate = alert;
    [alert show];
    self.alertViewCallBackBlock = alertViewCallBackBlock;
}

- (void)setAlertViewCallBackBlock:(UIAlertViewCallBackBlock)alertViewCallBackBlock {
    
    [self willChangeValueForKey:@"callbackBlock"];
    objc_setAssociatedObject(self, &UIAlertViewKey, alertViewCallBackBlock, OBJC_ASSOCIATION_COPY);
    [self didChangeValueForKey:@"callbackBlock"];
}

- (UIAlertViewCallBackBlock)alertViewCallBackBlock
{
    return objc_getAssociatedObject(self, &UIAlertViewKey);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (self.alertViewCallBackBlock) {
        self.alertViewCallBackBlock(buttonIndex);
    }
}


#pragma mark - iOS8 and later
+ (void)showAlertControllerPreferredStyle:(UIAlertControllerStyle)preferredStyle
                                withTitle:(NSString *)titlet
                                  message:(NSString *)message
                                  okTitle:(NSString *)okTitle
                         destructiveTitle:(NSString *)destructiveTitle
                              cancelTitle:(NSString *)cancelTitle
                                 okAction:(void (^)(UIAlertAction *action))okActe
                             cancelAction:(void (^)(void))cancelActe
                        destructiveAction:(void(^)(void))destructiveActe
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:titlet message:message preferredStyle:preferredStyle];
    
    // okAction
    if ((okTitle != nil) && [okTitle isEqualToString:@""]) {
        UIAlertAction *okAction = nil;
        if (okActe != nil) {
            
            okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                okActe(action);//有回调
            }];
            [alertVC addAction:okAction];
        }else {
            okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:nil];
            [alertVC addAction:okAction];
        }
    }
    
    // cancelAction
    if ((cancelTitle != nil) && ![cancelTitle isEqualToString:@""]) {
        
        UIAlertAction *cancelAction = nil;
        if (cancelActe != nil) {
            cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                cancelActe();//有回调
            }];
        }else {
            cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:nil];
        }
        [alertVC addAction:cancelAction];
    }
    
    if (preferredStyle == UIAlertControllerStyleActionSheet && ![destructiveTitle isEqualToString:@""] && destructiveTitle != nil) {
        // UIAlertActionStyleDestructive
        UIAlertAction *deatructive = [UIAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            destructiveActe();
        }];
        [alertVC addAction:deatructive];
    }
}



@end
